package cn.com.service.impl.cs;

import static cn.com.support.message.MessageCodeEnum.ERROR_DELETE;
import static cn.com.support.message.MessageCodeEnum.ERROR_INSERT;
import static cn.com.support.message.MessageCodeEnum.ERROR_SELECT;
import static cn.com.support.message.MessageCodeEnum.ERROR_UPDATE;
import static cn.com.support.message.MessageCodeEnum.INFO_SUCCEED;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.commons.Paginator;
import cn.com.commons.utils.NumberUtil;
import cn.com.dal.dao.cs.OutsidecostDAO;
import cn.com.dal.dataobject.cs.OutsidecostDO;
import cn.com.service.cs.OutsidecostService;
import cn.com.support.dal.DAOException;
import cn.com.support.message.MessageResult;

@Service
public class OutsidecostServiceImpl implements OutsidecostService {

	private final Logger logger = LoggerFactory
			.getLogger(ProjectServiceImpl.class);

	@Autowired
	private OutsidecostDAO<OutsidecostDO> outsidecostDAO;

	@Override
	public <C> Paginator<C, List<OutsidecostDO>> selectByPage(
			Paginator<C, List<OutsidecostDO>> paginator) {
		Paginator<C, List<OutsidecostDO>> result = null;
		try {
			result = outsidecostDAO.selectForPage(paginator);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		return result;
	}

	@Override
	public MessageResult<OutsidecostDO> selectById(Long id) {
		MessageResult<OutsidecostDO> result = new MessageResult<OutsidecostDO>(
				false, null, ERROR_SELECT);
		OutsidecostDO outsidecostDO = null;
		try {
			outsidecostDO = outsidecostDAO.selectById(id);
			result.setMessageResult(true, outsidecostDO, INFO_SUCCEED);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, outsidecostDO, ERROR_SELECT);
		}
		return result;
	}

	@Override
	public MessageResult<Long> insert(OutsidecostDO outsidecostDO) {
		MessageResult<Long> result = new MessageResult<Long>(false, null,
				ERROR_INSERT);
		try {
			Long id = outsidecostDAO.insert(outsidecostDO);
			if (id != null && id > 0) {
				result = new MessageResult<Long>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			logger.error(String.format(ERROR_INSERT.descript() + ":[%s]",
					outsidecostDO), e);
		}
		return result;
	}

	@Override
	public MessageResult<Integer> updateById(OutsidecostDO outsidecostDO) {
		MessageResult<Integer> result = new MessageResult<Integer>(false, null,
				ERROR_UPDATE);
		try {
			Integer id = outsidecostDAO.updateById(outsidecostDO);
			if (id != null && id > 0) {
				result = new MessageResult<Integer>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]",
					outsidecostDO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Long> deleteById(Long[] ids) {
		MessageResult<Long> result = new MessageResult<Long>(false, null,
				ERROR_DELETE);
		Integer count = null;
		for (int i = 0; i < ids.length; i++) {
			try {
				count = outsidecostDAO.deleteById(ids[i]);
				if (NumberUtil.isPositive(count)) {
					result = new MessageResult<Long>(true, ids[i], INFO_SUCCEED);
					continue;
				} else {
					// 删除失败，事物回滚
					result = new MessageResult<Long>(false, ids[i],
							ERROR_DELETE);
					TransactionAspectSupport.currentTransactionStatus()
							.setRollbackOnly();
					return result;
				}
			} catch (Exception e) {

				// 删除失败，事物回滚
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				result = new MessageResult<Long>(false, ids[i], ERROR_DELETE);
				logger.error(
						String.format(ERROR_DELETE.descript() + ":[%d]", i), e);
			}
		}
		return result;
	}

	@Override
	public List<OutsidecostDO> selectList(Map<Object, Object> params) {
		List<OutsidecostDO> result = null;
		try {
			result = outsidecostDAO.selectList(params);
		} catch (Exception e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

}
